package com.hao.product.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hao.common.code.ErrorCode;
import com.hao.common.execption.BusinessException;
import com.hao.common.response.BaseResponse;
import com.hao.common.utils.ResultUtils;
import com.hao.product.app.annotation.LoginCheck;
import com.hao.product.app.feign.UserFeign;
import com.hao.product.app.model.dto.product.ProductAddRequest;
import com.hao.product.app.model.dto.product.ProductQueryRequest;
import com.hao.product.app.model.dto.product.ProductUpdateRequest;
import com.hao.product.app.model.entity.Product;
import com.hao.product.app.model.vo.ProductVO;
import com.hao.product.app.service.ProductService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;

/**
 * @author haoge
 * @version 1.0
 * @date 2023/5/22 15:56
 */
@RestController
@RequestMapping ("/product")
@Slf4j
@Api (tags = "商品管理")
public class ProductController {

    @Resource
    private ProductService productService;

    @Resource
    private UserFeign userFeign;

    @PostMapping ("/add")
    @LoginCheck
    public BaseResponse<Boolean> addProduct(@RequestBody @Valid ProductAddRequest productAddRequest) {
        if (productAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        boolean result = productService.addProduct(productAddRequest);
        return ResultUtils.success(result);
    }

    @PostMapping ("/update")
    @LoginCheck
    public BaseResponse<Boolean> updateProduct(@RequestBody @Valid ProductUpdateRequest productUpdateRequest) {
        if (productUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = productService.updateProduct(productUpdateRequest);
        return ResultUtils.success(result);
    }

    @GetMapping ("/isLogin")
    public BaseResponse<Long> isLogin() {
        Long login = userFeign.isLogin();
        if (login == null) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "未登录");
        }
        return ResultUtils.success(login);
    }

    @GetMapping ("/get")
    public BaseResponse<ProductVO> getById(Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Product product = productService.getById(id);
        ProductVO productVO = new ProductVO();
        if (product == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "商品不存在");
        }
        try {
            BeanUtils.copyProperties(productVO, product);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return ResultUtils.success(productVO);
    }

    @GetMapping ("/list")
    public BaseResponse<Page<ProductVO>> listProductByPage(ProductQueryRequest productQueryRequest) {
        Page<ProductVO> productVOPage = productService.listByPage(productQueryRequest);
        return ResultUtils.success(productVOPage);
    }

    @PostMapping ("/delete")
    @LoginCheck
    public BaseResponse<Boolean> deleteById(Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = productService.removeById(id);
        if (!b) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(b);
    }

    @PostMapping ("/decrease")
    public Boolean decreaseProductStock(@RequestParam Long id,@RequestParam Integer count) {
        boolean result;
        try {
            result = productService.decreaseProductStock(id, count);
            return result;
        } catch (Exception e) {
            return false;
        }

    }
}
